package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.StoreInfoAdapter;
import com.da.lon.wang.xlg.bean.StoreInfoResult;
import com.da.lon.wang.xlg.bean.StoreInfoResult.StoreInfos;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.DialogUtil;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class StoreInfoActivity extends Activity implements OnClickListener {
    TextView title;
    PullToRefreshListView listView;
    Intent intent;
    DialogUtil dialogUtil;
    ImageButton left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_list);

        title = (TextView) findViewById(R.id.title);
        listView = (PullToRefreshListView) findViewById(R.id.store_list);
        dialogUtil = new DialogUtil(this);
        intent = getIntent();
        String name = intent.getStringExtra("store_name");
        if (name != null) {
            title.setText(intent.getStringExtra("store_name"));
        }
        left = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        left.setOnClickListener(this);
        getData(intent.getStringExtra("store_id"));
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void getData(String store_id) {

        HttpLoader<StoreInfoResult> loder = new HttpLoader<>(this, XLGConstants.URL + "/Store-getStoreAd",
                StoreInfoResult.class);
        AjaxParams params = new AjaxParams();
        params.put("token", XLGDataUtils.encrypt2MD5("Store" + XLGConstants.SECRET + "getStoreAd"));
        params.put("store_id", store_id);
        loder.loadData(params);

        loder.setManagerListener(new OnWebLoadListener<StoreInfoResult>() {

            @Override
            public void OnStart() {
                dialogUtil.showDialog();
            }

            @Override
            public void OnError(String error) {
                dialogUtil.dismissDialog();
            }

            @Override
            public void onSuccess(StoreInfoResult t) {
                dialogUtil.dismissDialog();
                ArrayList<StoreInfos> datas = t.getResult();
                StoreInfoAdapter adapter = new StoreInfoAdapter(datas, StoreInfoActivity.this);
                listView.setAdapter(adapter);
            }
        });


    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
